/**
 * 创建自定义组件的类
 */

package com.signway.javaui.component;

import com.signway.javaui.utils.SwLog;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

public class Ring extends Component implements Component.EstimateSizeListener, Component.DrawTask, Component.TouchEventListener {
    /* 自定义XML属性 */
    public static final String ATTR_RING_WIDTH = "ring_width";
    public static final String ATTR_RING_RADIUS = "ring_radius";
    public static final String ATTR_DEFAULT_COLOR = "default_color";
    public static final String ATTR_PRESSED_COLOR = "pressed_color";

    private float ringWidth = 20f;/* 圆环半径 */
    private float ringRadius = 200f;/* 圆环宽度 */
    private Color defaultColor = Color.YELLOW;/* 默认颜色 */
    private Color pressedColor = Color.CYAN;/* 按压态颜色 */
    private int width = 240;/* 默认宽度 */
    private int height = 240;/* 默认高度 */

    private Paint circlePaint;/* 绘制圆环的画笔 */

    public Ring(Context context) {
        super(context);
    }

    public Ring(Context context, AttrSet attrSet) {
        super(context, attrSet);

        /* 设置测量组件监听器 */
        setEstimateSizeListener(this);
        /* 初始化XMl属性 */
        initAttrSet(attrSet);
        /* 初始化画笔 */
        initPaint();
        /* 添加绘制任务 */
        addDrawTask(this);
        /* 设置TouchEvent响应事件 */
        setTouchEventListener(this);
    }

    private void initPaint() {
        circlePaint = new Paint();
        circlePaint.setColor(defaultColor);
        circlePaint.setStrokeWidth(ringWidth);
        circlePaint.setStyle(Paint.Style.STROKE_STYLE);
    }

    private void initAttrSet(AttrSet attrSet) {
        SwLog.info(this, "initAttrSet");
        if (attrSet == null) return;
        SwLog.info(this, "initAttrSet start");
        if (attrSet.getAttr(ATTR_DEFAULT_COLOR).isPresent()) {
            defaultColor = attrSet.getAttr(ATTR_DEFAULT_COLOR).get().getColorValue();
            SwLog.info(this, "defaultColor = " + defaultColor);
        }
        if (attrSet.getAttr(ATTR_RING_WIDTH).isPresent()) {
            ringWidth = attrSet.getAttr(ATTR_RING_WIDTH).get().getDimensionValue();
            SwLog.info(this, "ringWidth = " + ringWidth);
        }
        if (attrSet.getAttr(ATTR_RING_RADIUS).isPresent()) {
            ringRadius = attrSet.getAttr(ATTR_RING_RADIUS).get().getDimensionValue();
            SwLog.info(this, "ringRadius = " + ringRadius);
        }
        if (attrSet.getAttr(ATTR_PRESSED_COLOR).isPresent()) {
            pressedColor = attrSet.getAttr(ATTR_PRESSED_COLOR).get().getColorValue();
            SwLog.info(this, "pressedColor = " + pressedColor);
        }
    }

    @Override /* 组件测量 */
    public boolean onEstimateSize(int widthEstimateConfig, int heightEstimateConfig) {
        int widthSpec = EstimateSpec.getMode(widthEstimateConfig);/* 获取测量模式 */
        int heightSpec = EstimateSpec.getMode(heightEstimateConfig);/* 获取测量模式 */

        int widthConfig = 0;
        switch (widthSpec) {
            case EstimateSpec.UNCONSTRAINT:/* 父布局对子组件没有约束 */
            case EstimateSpec.PRECISE:/* 父布局已确定子组件大小 */
                width = EstimateSpec.getSize(widthEstimateConfig);
                widthConfig = EstimateSpec.getSizeWithMode(width, EstimateSpec.PRECISE);
                break;
            case EstimateSpec.NOT_EXCEED:/* 已为子组件确定了最大大小，子组件不能超过指定大小 */
                widthConfig = EstimateSpec.getSizeWithMode(width, EstimateSpec.PRECISE);
                break;
            default:
                break;
        }

        int heightConfig = 0;
        switch (heightSpec) {
            case EstimateSpec.UNCONSTRAINT:
            case EstimateSpec.PRECISE:
                height = EstimateSpec.getSize(heightEstimateConfig);
                heightConfig = EstimateSpec.getSizeWithMode(height, EstimateSpec.PRECISE);
                break;
            case EstimateSpec.NOT_EXCEED:
                heightConfig = EstimateSpec.getSizeWithMode(height, EstimateSpec.PRECISE);
                break;
            default:
                break;
        }
        setEstimatedSize(widthConfig, heightConfig);
        SwLog.info(this, "width = " + width);
        SwLog.info(this, "height = " + height);
        return true;
    }

    @Override/* 绘画组件 */
    public void onDraw(Component component, Canvas canvas) {
        int x = width / 2;
        int y = height / 2;
        canvas.drawCircle(x, y, ringRadius, circlePaint);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                circlePaint.setColor(pressedColor);
                invalidate();
                break;
            case TouchEvent.PRIMARY_POINT_UP:
                circlePaint.setColor(defaultColor);
                invalidate();
                break;
        }
        return true;
    }
}
